package murphy.validator;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import com.health.exceptions.ValidatorException;
import com.health.exceptions.ValidatorLevelException;

/**
 * @author DirkMurphy
 * 
 * @blog http://www.cnblogs.com/dirkmurphyjava/
 * 
 * @date
 * 
 * @introduction supply some simple operation about ClassObject
 */
public class ClassUtils
{

	/**
	 * @introduction params must be full ClassName, like
	 *               "java.lang.String","com.health.model.User"
	 * 
	 * @param sub
	 *            subclass classname
	 * @param sup
	 *            superclass classname
	 * @throws ClassNotFoundException
	 * */
	@SuppressWarnings("unchecked")
	public static boolean isInherit(String sub, String sup)
			throws ValidatorLevelException
	{
		boolean result = false;
		Class clazzSub = null;
		Class clazzSuper = null;
		try
		{
			clazzSub = Class.forName(sub);
			clazzSuper = Class.forName(sup);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			throw new ValidatorLevelException(e.getMessage(), e);
		}
		result = clazzSuper.isAssignableFrom(clazzSub);
		return result;
	}

	/**
	 * @param className
	 * 
	 * @throws ClassNotFoundException
	 * */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static boolean isNumber(String className)
			throws ValidatorLevelException
	{
		if (null == className)
		{
			return false;
		}
		else
		{
			// Integer, Double, Short, Float, Long, BigInteger, BigDecimal
			Class clazzNumber = null;
			Class clazzInput = null;
			boolean b = false;
			try
			{
				clazzNumber = Class.forName("java.lang.Number");
				clazzInput = Class.forName(className);
				b = clazzNumber.isAssignableFrom(clazzInput);
			}
			catch (ClassNotFoundException e)
			{
				String[] classesInNumberSimple = new String[] { "Integer",
						"Double", "Short", "Float", "Long", "BigInteger",
						"BigDecimal" };
				for (String classInNumberSimple : classesInNumberSimple)
				{
					if (classInNumberSimple.equals(className))
					{
						if (className.equals("BigInteger")
								|| className.equals("BigDecimal"))
						{
							className = "java.math." + className;
						}
						else
						{
							className = "java.lang." + className;
						}
						break;
					}
				}
				try
				{
					clazzInput = Class.forName(className);
				}
				catch (ClassNotFoundException e1)
				{
					e1.printStackTrace();
					throw new ValidatorLevelException(e1.getMessage(), e1);
				}
				if (clazzNumber.isAssignableFrom(clazzInput))
				{
					return true;
				}
				else
				{
					return false;
				}
			}
			return b;
		}
	}

	/**
	 * @param className
	 * 
	 * @throws ClassNotFoundException
	 * 
	 * */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static boolean isString(String className)
			throws ValidatorLevelException
	{
		boolean result = false;
		if (null == className)
		{
			return false;
		}
		else
		{
			Class clazzString = null;
			Class clazzInput = null;
			try
			{
				clazzString = Class.forName("java.lang.String");
				clazzInput = Class.forName(className);
				result = clazzString.isAssignableFrom(clazzInput);
			}
			catch (ClassNotFoundException e)
			{
				if (className.equals("String"))
				{
					className = "java.lang." + className;
				}
				try
				{
					clazzInput = Class.forName(className);
				}
				catch (ClassNotFoundException e1)
				{
					e1.printStackTrace();
					throw new ValidatorLevelException(e1.getMessage(), e1);
				}
				result = clazzString.isAssignableFrom(clazzInput);
				return result;
			}
		}
		return result;
	}

	/**
	 * @param className
	 * @param number
	 * @throws ValidatorLevelException
	 * 
	 * @intoduction change the Number Object to particular NumberFormat,like
	 *              "Integer,Double...",<br>
	 *              assignment the number input to it's particular Class
	 * */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void getParticularNumber(String className, Number number)
			throws ValidatorLevelException
	{
		Method parseMethod = null;
		String parseMethodName = "parse";
		String[] clazzStrArr = new String[] { "java.lang.Integer",
				"java.lang.Double", "java.lang.Float", "java.lang.Short",
				"java.lang.Long" };
		// ArrayList<Class> clazzList = new ArrayList<Class>();
		Class clazz = null;
		for (String clazzName : clazzStrArr)
		{
			if (className.equals(clazzName))
			{

			}
		}
	}

}
